<cfset model = deserializeJSON(generated_mdl) />
<cfset PrimaryKeyExists = false />
<cfloop collection="#model#" item="idx">
	<cfset props = model["#idx#"] />
</cfloop>

<cfoutput>
&lt;cfcomponent&gt;
	&lt;cffunction access="public" returntype="any" name="create"&gt;
		&lt;cfargument name="model_properties" type="Struct" /&gt;
		<cfset a = 0 /><cfset b = 0 />
		&lt;cfset var qryCreate="" /&gt;
			
		&lt;cfquery name="qryCreate" datasource="##application.env.dsn##"&gt;
			INSERT INTO #form.table_name#
			(
			<cfloop collection="#props#" item="columns">
			<cfif (structkeyExists(props[columns],'key') and props[columns]['key'] eq "primary")>
				<cfset PrimaryKeyExists = true />
				<cfif (not structkeyExists(props[columns],'identity')) or props[columns]['identity'] neq true>
					<cfif a neq 0>,</cfif>#columns#<cfset a = a+1/></cfif>	
			<cfelseif NOT (structkeyExists(props[columns],'key') and props[columns]['key'] eq "primary")><cfif a neq 0>,</cfif><cfset a=a+1 />#columns#
			<cfelse></cfif></cfloop>
			) 
			VALUES
			(
			<cfloop collection="#props#" item="columns">
			<cfif (structkeyExists(props[columns],'key') and props[columns]['key'] eq "primary")>
				<cfif (not structkeyExists(props[columns],'identity')) or props[columns]['identity']  neq true>
					<cfif b neq 0>,</cfif>
					&lt;cfqueryparam cfsqltype="cf_sql_#props[columns]['type']#" value="##model_properties.#columns#.value##" /&gt;<cfset b=b+1 />
				</cfif>
			<cfelseif  NOT (structkeyExists(props[columns],'key') and props[columns]['key'] eq "primary")><cfif b gt 0>,</cfif><cfset b = b+1 />&lt;cfqueryparam cfsqltype="cf_sql_#props[columns]['type']#" value="##model_properties.#columns#.value##" /&gt;
			</cfif></cfloop>
			)
		&lt;/cfquery&gt;
	&lt;/cffunction&gt;
<cfloop collection="#props#" item="column_name"><cfif structKeyExists(props[column_name], 'key') AND props[column_name]['key'] EQ 'foreign'>
	&lt;cffunction access="public" returntype="any" name="readBy#left(props[column_name]['references'],refind("\.",props[column_name]['references'])-1)#"&gt;
		&lt;cfargument name="model_foreign_key" type="numeric" /&gt;
		&lt;cfset var qryReadBy#left(props[column_name]['references'],refind("\.",props[column_name]['references'])-1)#="" /&gt;
		&lt;cfquery name="qryReadBy#left(props[column_name]['references'],refind("\.",props[column_name]['references'])-1)#" datasource="##application.env.dsn##"&gt;
			SELECT #form.table_name#.*, 
			#left(props[column_name]['references'],refind("\.",props[column_name]['references'])-1)#.*
			FROM #form.table_name# 
			<cfloop collection="#props#" item="fk_tbl_name">
				<cfif structKeyExists(props[fk_tbl_name], 'key') AND props[fk_tbl_name]['key'] EQ 'foreign'> INNER JOIN	#left(props[fk_tbl_name]['references'],refind("\.",props[fk_tbl_name]['references'])-1)# ON #form.table_name#.#column_name# = #props[fk_tbl_name]['references']#
			</cfif>
			</cfloop>
			WHERE #props[column_name]['references']# = &lt;cfqueryparam cfsqltype="cf_sql_#props[column_name]['type']#" value="##arguments.model_foreign_key##" /&gt;
		&lt;/cfquery&gt;		
		&lt;cfreturn qryReadBy#left(props[column_name]['references'],refind("\.",props[column_name]['references'])-1)# /&gt;
	&lt;/cffunction&gt;
</cfif></cfloop>
	&lt;cffunction access="public" returntype="any" name="readAll"&gt;
		&lt;cfset var qryReadAll="" /&gt;
		&lt;cfquery name="qryReadAll" datasource="##application.env.dsn##"&gt;
			SELECT #form.table_name#.* <cfloop collection="#props#" item="column_name"><cfif structKeyExists(props[column_name], 'key') AND props[column_name]['key'] EQ 'foreign'>, #left(props[column_name]['references'],refind("\.",props[column_name]['references'])-1)#.*</cfif></cfloop>
			FROM #form.table_name#
			<cfloop collection="#props#" item="column_name"><cfif structKeyExists(props[column_name], 'key') AND props[column_name]['key'] EQ 'foreign'> 
			INNER JOIN	#left(props[column_name]['references'],refind("\.",props[column_name]['references'])-1)# ON #form.table_name#.#column_name# = #props[column_name]['references']#
			</cfif></cfloop>
		&lt;/cfquery&gt;
		&lt;cfreturn qryReadAll /&gt;
	&lt;/cffunction&gt;
	
	&lt;cffunction access="public" returntype="any" name="read"&gt;
		&lt;cfargument name="model_properties" type="any" /&gt;
		&lt;cfset var qryRead="" /&gt;
		
		&lt;cfquery name="qryRead" datasource="##application.env.dsn##"&gt;
			SELECT #form.table_name#.* <cfloop collection="#props#" item="column_name"><cfif structKeyExists(props[column_name], 'key') AND props[column_name]['key'] EQ 'foreign'>, #left(props[column_name]['references'],refind("\.",props[column_name]['references'])-1)#.*</cfif></cfloop>
			FROM #form.table_name#
			<cfloop collection="#props#" item="column_name"><cfif structKeyExists(props[column_name], 'key') AND props[column_name]['key'] EQ 'foreign'> 
				INNER JOIN	#left(props[column_name]['references'],refind("\.",props[column_name]['references'])-1)# ON #form.table_name#.#column_name# = #props[column_name]['references']#
			</cfif></cfloop>
			WHERE 1=1
			<cfloop collection="#props#" item="column_name"><cfif structKeyExists(props[column_name], 'key') AND props[column_name]['key'] EQ 'primary'>
			AND #form.table_name#.#column_name# = &lt;cfqueryparam cfsqltype="cf_sql_#props[column_name]['type']#" value="##model_properties.#column_name#.value##" /&gt;
			</cfif><cfif structKeyExists(props[column_name], 'key') AND props[column_name]['key'] EQ 'foreign'  AND PrimaryKeyExists EQ false>
			AND #props[column_name]['references']# = &lt;cfqueryparam cfsqltype="cf_sql_#props[column_name]['type']#" value="##model_properties.#column_name#.value##" /&gt;
			</cfif></cfloop>
		&lt;/cfquery&gt;
		&lt;cfreturn qryRead /&gt;
	&lt;/cffunction&gt;
	
	&lt;cffunction access="public" returntype="any" name="update"&gt;
		&lt;cfargument name="model_properties" type="Struct" /&gt;
		&lt;cfset var qryUpdate="" /&gt;
		
		<cfset i = 0 />
		&lt;cfquery name="qryUpdate" datasource="##application.env.dsn##"&gt;
			UPDATE #form.table_name# 
			SET 
				<cfloop collection="#props#" item="columns"><cfif NOT (structkeyExists(props[columns],'key') and props[columns]['key'] eq "primary")>
				<cfif i GT 0>,</cfif><cfset i = i+1 />#columns# = &lt;cfqueryparam cfsqltype="cf_sql_#props[columns]['type']#" value="##model_properties.#columns#.value##" /&gt;
				</cfif></cfloop>
			WHERE 1=1
			<cfloop collection="#props#" item="column_name"><cfif structKeyExists(props[column_name], 'key') AND props[column_name]['key'] EQ 'primary'>
			AND #form.table_name#.#column_name# = &lt;cfqueryparam cfsqltype="cf_sql_#props[column_name]['type']#" value="##model_properties.#column_name#.value##" /&gt;
			</cfif><cfif structKeyExists(props[column_name], 'key') AND props[column_name]['key'] EQ 'foreign' AND PrimaryKeyExists EQ false>
			AND #form.table_name#.#column_name# = &lt;cfqueryparam cfsqltype="cf_sql_#props[column_name]['type']#" value="##model_properties.old_#column_name#.value##" /&gt;
			</cfif></cfloop>
		&lt;/cfquery&gt;
	&lt;/cffunction&gt;
	
	&lt;cffunction access="public" returntype="any" name="delete"&gt;
		&lt;cfargument name="model_properties" type="Struct" /&gt;
		&lt;cfargument name="disable_only" type="boolean" default="false" /&gt;
		&lt;cfset var qryDelete="" /&gt;
		
		&lt;cfif isDefined('arguments.disable_only') AND arguments.disable_only eq true&gt;
			&lt;cfquery name="qryDelete" datasource="##application.env.dsn##"&gt;
				UPDATE #form.table_name# 
				SET is_active = false
				WHERE 1=1
				<cfloop collection="#props#" item="column_name"><cfif structKeyExists(props[column_name], 'key') AND props[column_name]['key'] EQ 'primary'>
				AND #form.table_name#.#column_name# = &lt;cfqueryparam cfsqltype="cf_sql_#props[column_name]['type']#" value="##model_properties.#column_name#.value##" /&gt;
				</cfif><cfif structKeyExists(props[column_name], 'key') AND props[column_name]['key'] EQ 'foreign' AND PrimaryKeyExists EQ false>
				AND #form.table_name#.#column_name# = &lt;cfqueryparam cfsqltype="cf_sql_#props[column_name]['type']#" value="##model_properties.#column_name#.value##" /&gt;
				</cfif></cfloop>
			&lt;/cfquery&gt;
		&lt;cfelse&gt;
			&lt;cfquery name="qryDelete" datasource="##application.env.dsn##"&gt;
				DELETE 
				FROM #form.table_name#
				WHERE 1=1
				<cfloop collection="#props#" item="column_name"><cfif structKeyExists(props[column_name], 'key') AND props[column_name]['key'] EQ 'primary'>
				AND #form.table_name#.#column_name# = &lt;cfqueryparam cfsqltype="cf_sql_#props[column_name]['type']#" value="##model_properties.#column_name#.value##" /&gt;
				</cfif><cfif structKeyExists(props[column_name], 'key') AND props[column_name]['key'] EQ 'foreign' AND PrimaryKeyExists is false>
				AND #form.table_name#.#column_name# = &lt;cfqueryparam cfsqltype="cf_sql_#props[column_name]['type']#" value="##model_properties.#column_name#.value##" /&gt;
				</cfif></cfloop>
			&lt;/cfquery&gt;
		&lt;/cfif&gt;
	&lt;/cffunction&gt;
&lt;/cfcomponent&gt;
</cfoutput>